using System.Collections.Generic;
using System.Linq;
using System.Windows.Browser;
using System.Windows.Media.Imaging;
using MediaOwl.LastFm.Model;

namespace MediaOwl.LastFm.Data
{
    public class Helper
    {
        internal static string GetNodeName<TEntity>()
        {
            var type = typeof(TEntity);
            if (type == typeof(Album))
            {
                return LastFmDataAccess.NodeNameOfAlbum;
            }
            if (type == typeof(Artist) || type == typeof(ArtistBase))
            {
                return LastFmDataAccess.NodeNameOfArtist;
            }
            if (type == typeof(Biography))
            {
                return LastFmDataAccess.NodeNameOfBiography;
            }
            if (type == typeof(Track))
            {
                return LastFmDataAccess.NodeNameOfTrack;
            }
            if (type == typeof(Tag))
            {
                return LastFmDataAccess.NodeNameOfTag;
            }

            return null;
        }

        /// <summary>
        /// Builds the url-search-string for the Last.Fm-REST-service
        /// </summary>
        /// <param name="parameters">The parameters defining the search</param>
        /// <returns>The search-url-string</returns>
        internal static string BuildSearchString(IEnumerable<Parameter> parameters)
        {
            const char starter = '?', and = '&', equal = '=';
            string searchString = LastFmDataAccess.BaseUri + starter;

            var paramList = new List<Parameter>(parameters);

            if (!paramList.Any(x => x.Name == LastFmDataAccess.ApiKey))
            {
                paramList.Add(new Parameter
                {
                    Name = LastFmDataAccess.ParamApiKey,
                    Value = LastFmDataAccess.ApiKey
                });
            }

            foreach (var p in paramList)
            {
                if (!searchString.EndsWith(starter.ToString()))
                    searchString += and;

                if (!string.IsNullOrEmpty(p.Name) || !string.IsNullOrEmpty(p.Value))
                    searchString += p.Name + equal + HttpUtility.UrlEncode(p.Value);
            }

            return searchString;
        }

        #region GetScreenIds

        internal static string GetScreenId(object o)
        {
            if (o is Album)
                return GetScreenId((Album)o);

            if (o is ArtistBase)
                return GetScreenId((ArtistBase)o);

            if (o is Track)
                return GetScreenId((Track)o);

            if (o is Tag)
                return GetScreenId((Tag)o);

            if (o is BitmapImage)
                return ((BitmapImage) o).UriSource.AbsoluteUri;

            if (o is string)
                return (string)o;

            return o.GetHashCode().ToString();
        }

        internal static string GetScreenId(Album album)
        {
            return string.IsNullOrEmpty(album.MusicBrainzId)
                ? album.Name + album.ArtistName
                : album.MusicBrainzId;
        }

        internal static string GetScreenId(ArtistBase artist)
        {
            return string.IsNullOrEmpty(artist.MusicBrainzId)
                ? artist.Name
                : artist.MusicBrainzId;
        }

        internal static string GetScreenId(Track track)
        {
            return string.IsNullOrEmpty(track.MusicBrainzId)
                ? track.Name +track.AlbumName + track.ArtistName
                : track.MusicBrainzId;
        }

        internal static string GetScreenId(Tag tag)
        {
            return tag.Name;
        }

        #endregion

    }
}